package com.maple.common.widget.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import com.maple.common.R
import com.maple.utils.ext.layoutInflater
import com.maple.utils.util.LogUtils
import com.maple.utils.util.ToastUtils

class PaginationLayout: LinearLayout {
    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView(
            context,
            attributeSet,
            defStyleAttr
        )
    }

    private var btnStart: Button? = null
    private var btnPrev: Button? = null
    private var btnCount: Button? = null
    private var btnNext: Button? = null
    private var btnEnd: Button? = null

    private var listener: OnPaginationListener? = null

    private var limit: Long = 8
    private var currentPage: Long = 0

    private var totalSize: Long = 0
    private var totalPage: Long = 0
    private var index: Long = 0

    fun setOnPaginationListener(l: OnPaginationListener?) {
        l?.let {
            this.listener = l
        }
    }

    private fun initView(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int = 0) {
        val view: View = context.layoutInflater.inflate(R.layout.layout_pagination, this)

//        attributeSet?.let {
//            val typedArray: TypedArray = context.obtainStyledAttributes(it, R.styleable.PaginationLayout)
//            typedArray.let { ta ->
//                ta.recycle()
//            }
//        }

        btnStart = view.findViewById(R.id.btn_start)
        btnPrev = view.findViewById(R.id.btn_prev)
        btnCount = view.findViewById(R.id.btn_count)
        btnNext = view.findViewById(R.id.btn_next)
        btnEnd = view.findViewById(R.id.btn_end)
        btnStart?.setOnClickListener {
            if(currentPage <= 1) {
                ToastUtils.showToast("已经是首页了")
                return@setOnClickListener
            }
            val s: Long = 0
            val l: Long = limit
            LogUtils.logGGQ("首页 start:${s} --- limit:${l}")
            listener?.onStartPage(s,l)
        }
        btnPrev?.setOnClickListener {
            if(currentPage <= 1) {
                ToastUtils.showToast("已经是首页了")
                return@setOnClickListener
            }
            val s: Long = index - limit
            val l: Long = limit
            LogUtils.logGGQ("上一页 start:${s} --- limit:${l}")
            listener?.onPrevPage(s,l)
        }
        btnNext?.setOnClickListener {
            if(currentPage >= totalPage) {
                ToastUtils.showToast("已经是尾页了")
                return@setOnClickListener
            }
            val s: Long = currentPage * limit
            val l: Long = limit
            LogUtils.logGGQ("下一页 start:${s} --- limit:${l}")
            listener?.onNextPage(s,l)
        }
        btnEnd?.setOnClickListener {
            if(currentPage >= totalPage) {
                ToastUtils.showToast("已经是尾页了")
                return@setOnClickListener
            }
            val s: Long = (totalPage - 1) * limit
            val l: Long = limit
            LogUtils.logGGQ("尾页 start:${s} --- limit:${l}")
            listener?.onEndPage(s,l)
        }
    }


    private fun setCountPage() {
        currentPage = if(index <= 0) 1 else (index / limit) + 1
        btnCount?.text = "${currentPage}/${totalPage}"
    }

    fun reset(index: Long,limit: Long) {
        this.index = index
        this.limit = limit
    }

    fun getIndex(): Long = index
    fun getLimit(): Long = limit


    fun setPageData(index: Long, count: Long, size: Long) {
        this.index = index
        this.totalPage = count
        this.totalSize = size
        setCountPage()
    }


    interface OnPaginationListener {

        fun onStartPage(start: Long, limit: Long)
        fun onPrevPage(start: Long, limit: Long)
        fun onNextPage(start: Long, limit: Long)
        fun onEndPage(start: Long, limit: Long)
    }


}